import { _ as _class_call_check } from "@swc/helpers/_/_class_call_check";
import { _ as _create_class } from "@swc/helpers/_/_create_class";
import { _ as _define_property } from "@swc/helpers/_/_define_property";
import { _ as _instanceof } from "@swc/helpers/_/_instanceof";
import { _ as _object_spread } from "@swc/helpers/_/_object_spread";
import { loadJS } from "./utils";
import { FACEBOOK_APPID } from "./config";
var FaceBookAuth = /*#__PURE__*/ function _target() {
    "use strict";
    function FaceBookAuth() {
        var _this = this;
        _class_call_check(this, FaceBookAuth);
        _define_property(this, "load", false);
        _define_property(this, "getCode", function() {
            return new Promise(function(resolve, reject) {
                /** facebook 要求使用https 协议  回调地址需要在 管理界面添加    */ // OAuth2Window.open(
                //   'facebook',
                //   `https://www.facebook.com/v16.0/dialog/oauth?client_id=${FACEBOOK_APPID}&redirect_uri=${encodeURIComponent(
                //     REDIRECT_URI,
                //   )}&state={}&response_type=code`,
                //   {
                //     width: '600',
                //     height: '500',
                //   },
                // ).then((res) => {
                //   console.log(res)
                // }, reject)
                _this.init().then(function() {
                    var _window_FB;
                    (_window_FB = window.FB) === null || _window_FB === void 0 ? void 0 : _window_FB.login(function(response) {
                        console.log(response);
                        if (response.authResponse) {
                            resolve(_object_spread({
                                code: response.authResponse.accessToken,
                                state: ""
                            }, response.authResponse));
                        }
                        reject(response);
                    }, {
                        return_scopes: true
                    });
                }, reject);
            });
        });
        _define_property(this, "getAccessToken", function() {
            return new Promise(function(resolve, reject) {
                try {
                    _this.getCode().then(function(res) {
                        var code = res.code;
                    });
                } catch (err) {
                    reject(err);
                }
            });
        });
        if ((_instanceof(this, FaceBookAuth) ? this.constructor : void 0) !== FaceBookAuth) {
            return;
        }
        if (!FaceBookAuth._instance) {
            FaceBookAuth._instance = this;
        }
        /** 加载sdk */ this.loadFBSdk();
        return FaceBookAuth._instance;
    }
    _create_class(FaceBookAuth, [
        {
            key: "loadFBSdk",
            value: function loadFBSdk(cb, onError) {
                var _this = this;
                loadJS("https://connect.facebook.net/en_US/sdk.js", function() {
                    var _this1, _this_onLoad;
                    window.FB.init({
                        appId: FACEBOOK_APPID,
                        cookie: true,
                        // Enable cookies to allow the server to access the session.
                        xfbml: true,
                        // Parse social plugins on this webpage.
                        version: "v16.0"
                    });
                    _this.load = true;
                    (_this_onLoad = (_this1 = _this).onLoad) === null || _this_onLoad === void 0 ? void 0 : _this_onLoad.call(_this1);
                    cb === null || cb === void 0 ? void 0 : cb();
                }, function() {
                    var _this1, _this_onError;
                    onError === null || onError === void 0 ? void 0 : onError();
                    (_this_onError = (_this1 = _this).onError) === null || _this_onError === void 0 ? void 0 : _this_onError.call(_this1);
                });
            }
        },
        {
            key: "init",
            value: function init() {
                var _this = this;
                /** 假如 sdk 加载失败可以使用init 重新加载sdk */ return new Promise(function(resolve, reject) {
                    if (_this.load && window.FB) {
                        resolve(FaceBookAuth._instance);
                        return;
                    }
                    _this.loadFBSdk(function() {
                        resolve(FaceBookAuth._instance);
                    }, reject);
                });
            }
        }
    ], [
        {
            key: "getInstance",
            value: function getInstance() {
                if (!FaceBookAuth._instance) {
                    FaceBookAuth._instance = new FaceBookAuth();
                }
                return FaceBookAuth._instance;
            }
        }
    ]);
    return FaceBookAuth;
}();
_define_property(FaceBookAuth, "_instance", null);
export default FaceBookAuth.getInstance();

 //# sourceMappingURL=facebook.js.map